package com.sleepingcat.ft_jetpack.life_cycle

import android.content.Context
import android.os.SystemClock
import android.util.AttributeSet
import android.widget.Chronometer
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent

class MyChronometer : Chronometer, DefaultLifecycleObserver {

    private var elapsedTime: Long = 0

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        elapsedTime = SystemClock.elapsedRealtime() - base
    }

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        base = SystemClock.elapsedRealtime() - elapsedTime
        start()
    }

}